package com.zt.service.code.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.sql.DataSource;

import org.springframework.jdbc.support.rowset.SqlRowSetMetaData;

import com.zt.dao.code.IGenJdbcCodeDao;
import com.zt.service.code.IGenJdbcCodeService;

/**
 * Service类 GenJdbcCodeServiceImpl
 * 
 * @author zhangzhw
 * @describe 取得列信息的Serivce类
 */
public class GenJdbcCodeServiceImpl implements IGenJdbcCodeService {

	public IGenJdbcCodeDao iGenJdbcCodeDao;

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.zt.service.code.impl.IGenJdbcCodeService#getMetaData(java.lang.String
	 * )
	 */
	public Map<String, List<Object>> getMetaData(String sql) {
		Map<String, List<Object>> map = new HashMap<String, List<Object>>();
		SqlRowSetMetaData smd = iGenJdbcCodeDao.getMetaData(sql);

		List<Object> columnNames = new ArrayList<Object>();
		List<Object> columnTypes = new ArrayList<Object>();
		List<Object> columnTypeNames = new ArrayList<Object>();
		List<Object> columnClassNames = new ArrayList<Object>();
		List<Object> columnPrecisions = new ArrayList<Object>();
		List<Object> scals = new ArrayList<Object>();
		List<Object> schemas = new ArrayList<Object>();
		List<Object> tables = new ArrayList<Object>();

		for (int i = 1; i <= smd.getColumnCount(); i++) {
			columnNames.add(smd.getColumnName(i));
			columnTypes.add(smd.getColumnType(i));
			columnTypeNames.add(smd.getColumnTypeName(i));
			columnClassNames.add(smd.getColumnClassName(i));
			columnPrecisions.add(smd.getPrecision(i));
			scals.add(smd.getScale(i));
			schemas.add(smd.getSchemaName(i));
			tables.add(smd.getTableName(i));
		}

		map.put("columnNames", columnNames);
		map.put("columnTypes", columnTypes);
		map.put("columnTypeNames", columnTypeNames);
		map.put("columnClassNames", columnClassNames);
		map.put("columnPrecisions", columnPrecisions);
		map.put("scals", scals);
		map.put("schemas", schemas);
		map.put("tables", tables);

		return map;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.zt.service.code.IGenJdbcCodeService#testDataSource(java.lang.String,
	 * java.lang.String, java.lang.String)
	 */
	public DataSource testDataSource(String jdbcUrl, String user,
			String password) throws Exception {
		return iGenJdbcCodeDao.testDataSource(jdbcUrl, user, password);
	}

	// getters and setters

	public IGenJdbcCodeDao getiGenJdbcCodeDao() {
		return iGenJdbcCodeDao;
	}

	public void setiGenJdbcCodeDao(IGenJdbcCodeDao iGenJdbcCodeDao) {
		this.iGenJdbcCodeDao = iGenJdbcCodeDao;
	}

}
